package edu.jsnu.service.ticket.film.service.impl;

import beans.PageQuery;
import com.github.pagehelper.PageHelper;
import edu.jsnu.service.ticket.film.dao.FilmMapper;
import edu.jsnu.service.ticket.film.domain.Film;
import edu.jsnu.service.ticket.film.dto.FilmBrief;
import edu.jsnu.service.ticket.film.dto.FilmDto;
import edu.jsnu.service.ticket.film.dto.FilmReceive;
import edu.jsnu.service.ticket.film.service.FilmService;
import edu.jsnu.service.ticket.order.dao.OrderMapper;
import edu.jsnu.service.ticket.schedule.dao.ScheduleMapper;
import edu.jsnu.service.ticket.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import util.CommonVariable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@EnableScheduling
@Service
public class FilmServiceImpl implements FilmService {

    @Autowired
    private FilmMapper filmMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Override
    public List<FilmDto> getAllFilms(PageQuery pageQuery, Integer status, String keyword) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getLimit());
        List<FilmDto> filmList = filmMapper.getAllFilms(status, keyword);
        return filmList;
    }

    @Override
    public Film getFilmById(Integer id) {
        Film film = filmMapper.selectByPrimaryKey(id);
        return film;
    }

    @Override
    public Integer  insertFilm(FilmReceive receive, User currentUser) {
        Film film = FilmReceive.Receive2Film(receive);
        film.setStatus(1);
        film.setOperatorId(currentUser.getId());
        film.setOperatorName(currentUser.getUserName());
        film.setOperatorTime(new Date());
        Integer result = filmMapper.insertSelective(film);
        return result;
    }

    @Override
    public Integer updateFilm(FilmReceive receive, User currentUser) {
        Film film = FilmReceive.Receive2Film(receive);
        film.setOperatorId(currentUser.getId());
        film.setOperatorName(currentUser.getUserName());
        film.setOperatorTime(new Date());
        Integer result = filmMapper.updateByPrimaryKeySelective(film);
        return result;
    }

    public final long oneMonthMills = 30 * 24 * 60 * 60 * 1000;
    //影片自动下架定时器  0：下架 1：上线  影片到一个月之后自动下架，每天23:59分检测
    @Scheduled(cron = "0  0  0  *  *  ? ")
    public void updateFilmByStatus(){
        Integer status = 1;
        //拿到所有状态为1的影片
        List<Film> films = filmMapper.getFilmByStatus(status);
        //判断影片影院上映时间与当前时间间隔是否大于一个月的毫秒数
        for (Film f : films){
            if (new Date().getTime() - f.getOnlineTime().getTime() > oneMonthMills){
                f.setStatus(0);
                //写入新的记录到数据库
                filmMapper.updateByPrimaryKey(f);
            }
        }
    }

    //影片删除需先删除外键关系
    @Override
    public Integer deleteFilm(Integer id) {
        //先删除order表外键约束
        orderMapper.deleteByFilmId(id);
        //再删除schedule外键约束
        scheduleMapper.deleteByFilmId(id);
        //删除film
        Integer result = filmMapper.deleteByPrimaryKey(id);
        return result;
    }

    @Override
    public Integer batchDel(Integer[] ids) {
        Map<String, Integer[]> map = new HashMap<>();
        map.put("ids", ids);
        Integer result = filmMapper.batchDel(map);
        return result;
    }

    @Override
    public Integer updateStatus(Integer id, Integer status) {
        Integer result = filmMapper.updateStatus(id, status);
        return result;
    }

    @Override
    public List<FilmBrief> getBriefFilms(Integer page, Integer pageCount, Integer flag, String kind, String year) {
        Integer startPage = (page - 1)*pageCount;
        List<FilmBrief> filmList = filmMapper.getBriefFilmInfo(startPage, pageCount, flag, kind, year);
        return filmList;
    }

    @Override
    public Map getFilmListByTypeOrYears(String filmType, String curPage, String filmYears) {
        List<Film> filmList = new ArrayList<>();
        //PageInfo pageInfo = new PageInfo();
        Map map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY:mm:dd");
        Date date = null;
        if (filmYears != null && !filmYears.equals("")){
            try {
                date = sdf.parse(filmYears);
            } catch (ParseException e) {
                System.out.println("日期转换错误");
            }
        }
        filmList = filmMapper.getFilmListByTypeOrYears(filmType, date, 1, CommonVariable.pageTotal);
        try {
            System.out.println(sdf.parse("2018-03-13") + "++++++++++++++++++++++++");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        map.put("filmList", filmList);
        return map;
    }
}
